package com.poitest.controller;

import com.poitest.pojo.TPoor;
import com.poitest.pojo.TTitle;
import com.poitest.service.PoorService;
import excel.ExcelUtils;
import excel.ImportRspInfo;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailParseException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("poor")
public class PoorController {

    @Autowired
    PoorService poorService;

    @RequestMapping("daoru")
    public String  parseSheet() throws Exception {
        // 1.获取源文件
        Workbook wb = WorkbookFactory.create(new FileInputStream("D:\\zshymx.xlsx"));
        // 2.获取sheet0导入
        Sheet sheet = wb.getSheetAt(0);
        // 3.生成VO实体
        ImportRspInfo<TPoor> list = ExcelUtils.parseSheet(TPoor.class, EvaluateVerifyBuilder2.getInstance(), sheet, 1, 0);

        boolean f = poorService.addAll(list);
        if (f) {
            return "people";
        }else {
            return "error" ;
        }
    }
    @RequestMapping("list")
    @ResponseBody
    public List<TPoor> list(){
        return poorService.selectByExample(null);
    }
    @RequestMapping("lists")
    public String lists(Integer id , Model model){
       TTitle tilte =  poorService.findOne(id);
        List<TPoor> tPoors = poorService.selectByExample(null);
        model.addAttribute("listAll",tPoors);
        model.addAttribute("title",tilte);
        return "toupiao";
    }
    @RequestMapping("dataList")
    @ResponseBody
    public Map dataList(){
        Map map = new HashMap();
        List<TPoor> o = poorService.PoorList();
        List<Object> name=new ArrayList<>();
        List<Object> psize=new ArrayList<>();



        for(int i = 0; i < o.size(); i++){
            name.add(o.get(i).getName());
            psize.add(o.get(i).getPsize());
        }
        map.put("name1",name);
        map.put("list",psize);
        return map;
    }
}
